$(function() {
	$('#error_tips').hide();
	$(document).pngFix();
	var path = $('#path').val();
	$("#chngVerification").click(
			function() {
				$("#verification").attr(
						'src',
						"verification.action?_d="
								+ new Date().getMilliseconds());
			});
	$("#loginButton").click(function() {
		if($("#user_name").val() == "" || $("#user_password").val() == ""){
			$('#error_tips').show();
			$('#error_tips').html("用户名或密码不能为空！");
			return false;
		}else if($("#verificationField").val() == ""){
			$('#error_tips').show();
			$('#error_tips').html("请输入验证码！");
			return false;
		}
		var url = 'login';
		var data = $("#loginForms").ajaxForm().formSerialize();
		$.post(url, data, function(json) {
			if (json.result) {
				if (json.mobileCode) {
					$.post('generalMobileVerification', function(mobileJson) {
						if (mobileJson.result) {
							$('#mobileVerification').dialog("open");
						} else {
							jAlert('发送验证码到手机失败!');
						}
					});
				} else {
					window.location = path + "/index.jsp";
				}
			} else {
				$('#error_tips').show();
				
				if(json.loginMessage != null && json.loginMessage != ''){
					$('#error_tips').html(json.loginMessage);
				}else{
					$('#error_tips').html('登录失败请稍后再试！');
				}
				
			}
		});
	});
	$("#resetButton").click(function() {
		$("#loginForms").ajaxForm().clearForm();
	});
	
	$("input").keydown(function(event){
		if(event.keyCode == 13){
			$("#loginButton").click();
		}
	});
	
	
});

function alterFocus(ev) {
	var ev = ev || window.event;
	if (ev.keyCode == 13) {
		$("#user_password").focus();
	}
}

function alterVerificationFocus(ev) {
	var ev = ev || window.event;
	if (ev.keyCode == 13) {
		$("#verificationField").focus();
	}
}

function submitForm(ev) {
	var ev = ev || window.event;
	var path = $('#path').val();
	if (ev.keyCode == 13) {
		//		if (!validationMutityEngine('loginForms'))
		//			return false;
		var url = 'login';
		var data = $("#loginForms").ajaxForm().formSerialize();
		$.post(url, data, function(json) {
			if (json.result) {
				if (json.mobileCode) {
					$.post('generalMobileVerification', function(mobileJson) {
						if (mobileJson.result) {
							$('#mobileVerification').dialog("open");
						} else {
							jAlert('发送验证码到手机失败!');
						}
					});
				} else {
					//				window.location = path + "/client.jsp";
				window.location = path + "/index.jsp";
			}
		} else {
			//$.messager.alert("提示消息", json.loginMessage, 'error');
//			alert(json.loginMessage)
			$('#error_tips').show();
			$('#error_tips').html(json.loginMessage);
		}
	}	);
	}
}
/**
 * 多个字段验证
 * 
 * @param formId
 * @return
 */
function validationMutityEngine(formId) {
	var result = true;
	$('#' + formId + ' tr td input').each(function() {
		var validationClass = $(this).attr('class');
		if (validationClass.indexOf('validate') > -1) {
			var idName = $(this).attr('id');
			if ($('#' + idName).attr('disbled') != 'disabled') {
				if ($.validationEngine.loadValidation("#" + idName))
					result = false;
			}
		}
	});
	return result;
}